package com.easy.project.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.project.base.exception.ServiceException;
import com.easy.project.convert.UserDomainConvert;
import com.easy.project.domain.dto.system.UserDTO;
import com.easy.project.domain.dto.system.UserEditDTO;
import com.easy.project.domain.po.system.SysUserPO;
import com.easy.project.domain.vo.system.SysUserInfoVO;
import com.easy.project.domain.vo.system.SysUserVO;
import com.easy.project.mapper.system.SysUserMapper;
import com.easy.project.service.system.ISysUserService;
import com.easy.project.util.PageUtils;
import com.easy.project.util.SaTokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

import static com.easy.project.base.result.SystemErrorCodeEnum.DATA_FIND_ERROR;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author wxz
 * @since 2024-01-10
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserPO> implements ISysUserService {

    @Resource
    private UserDomainConvert userDomainConvert;

    @Override
    public IPage<SysUserVO> userPage(UserDTO userDTO) {
        Page<SysUserPO> page = new PageUtils<SysUserPO,UserDTO>().buildPage(userDTO);
        LambdaQueryWrapper<SysUserPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(userDTO.getUserName()),SysUserPO::getUserName,userDTO.getUserName());
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(userDTO.getName()),SysUserPO::getName,userDTO.getName());
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(userDTO.getIdNumber()),SysUserPO::getIdNumber,userDTO.getIdNumber());
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(userDTO.getPhoneNumber()),SysUserPO::getPhoneNumber,userDTO.getPhoneNumber());
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(userDTO.getMail()),SysUserPO::getMail,userDTO.getMail());
        lambdaQueryWrapper.eq(Objects.nonNull(userDTO.getSex()),SysUserPO::getSex,userDTO.getSex());
        lambdaQueryWrapper.eq(Objects.nonNull(userDTO.getBirthday()),SysUserPO::getBirthday,userDTO.getBirthday());
        lambdaQueryWrapper.eq(Objects.nonNull(userDTO.getStatus()),SysUserPO::getStatus,userDTO.getStatus());
        page= this.page(page, lambdaQueryWrapper);
        return userDomainConvert.poPageToVoPage(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userEdit(UserEditDTO userEditDTO) {
        Long id = userEditDTO.getId();
        // todo 验证用户登录名,手机号,身份证是否重复 用户是否可用
        SysUserPO sysUserPO;
        if (Objects.isNull(id)){
            // 新增操作
            sysUserPO = userDomainConvert.editDtoToPo(userEditDTO);
            // 设置密码
            sysUserPO.setPassword(SaTokenUtils.generateIdNumberPassword(userEditDTO.getIdNumber()));
        }else {
            // 编辑操作
            sysUserPO = getById(id);
            if (Objects.isNull(sysUserPO)){
                throw new ServiceException(DATA_FIND_ERROR);
            }
            userDomainConvert.editDtoToPo(userEditDTO,sysUserPO);
        }
        saveOrUpdate(sysUserPO);
    }

    @Override
    public SysUserInfoVO userInfo(Long id) {
        SysUserPO userPO = getById(id);
        return userDomainConvert.poToInfoVo(userPO);
    }
}
